import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import Index from '../views/Index.vue'
import BusinessList from '../views/BusinessList.vue'
import OrderList from '../views/OrderList.vue'
import BusinessInfo from '../views/BusinessInfo.vue'
import Payment from '../views/Payment.vue'
import Order from '../views/Order.vue'

const routes: Array<RouteRecordRaw> = [
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    },
    {
        path: '/index',
        name: 'Index',
        component: Index
    },
    {
        path: '/business-list',
        name: 'BusinessList',
        component: BusinessList
    },
    {
        path: '/orders',
        name: 'OrderList',
        component: OrderList
    },
    {
        path: '/business/:id',
        name: 'BusinessInfo',
        component: BusinessInfo
    },
    {
        path: '/payment',
        name: 'Payment',
        component: Payment
    },
    {
        path: '/order',
        name: 'Order',
        component: Order
    },
    {
        path: '/',
        redirect: '/index'
    },
    {
        path: '/:pathMatch(.*)*',
        redirect: '/index'
    }
]

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes,
    // 配置滚动行为：每次路由跳转都回到页面顶部
    scrollBehavior(to, from, savedPosition) {
        // 如果有保存的位置且是通过浏览器前进后退按钮导航，则恢复位置
        if (savedPosition) {
            return savedPosition
        }
        // 否则始终滚动到页面顶部
        return { top: 0, left: 0 }
    }
})

export default router